package com.ruoyi.project.NH.farmer.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.NH.base_family_info.domain.BaseFamilyInfo;
import com.ruoyi.project.NH.base_family_info.service.IBaseFamilyInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.NH.farmer.domain.Farmer;
import com.ruoyi.project.NH.farmer.service.IFarmerService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 农户基本信息Controller
 *
 * @author 狗熊岭
 * @date 2023-07-24
 */
@RestController
@RequestMapping("/NH/farmer")
public class FarmerController extends BaseController
{
    @Autowired
    private IFarmerService farmerService;
    @Autowired
    private IBaseFamilyInfoService baseFamilyInfoService;

    /**
     * 查询农户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('NH:farmer:list')")
    @GetMapping("/list")
    public TableDataInfo list(Farmer farmer)
    {
        startPage();
        List<Farmer> list = farmerService.selectFarmerList(farmer);
        return getDataTable(list);
    }

    /**
     * 导出农户基本信息列表
     */
    @PreAuthorize("@ss.hasPermi('NH:farmer:export')")
    @Log(title = "农户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Farmer farmer)
    {
        List<Farmer> list =farmerService.selectFarmerList(farmer);
        ExcelUtil<Farmer> util = new ExcelUtil<Farmer>(Farmer.class);
        util.exportExcel(response, list, "农户基本信息数据");
    }

    /**
     * 获取农户基本信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('NH:farmer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(farmerService.selectFarmerById(id));
    }

    /**
     * 新增农户基本信息
     */
    @PreAuthorize("@ss.hasPermi('NH:farmer:add')")
    @Log(title = "新增农户基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Farmer farmer)
    {
        return toAjax(farmerService.insertFarmer(farmer));
    }

    /**
     * 修改农户基本信息
     */
    @PreAuthorize("@ss.hasPermi('NH:farmer:edit')")
    @Log(title = "修改农户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Farmer farmer)
    {
        return toAjax(farmerService.updateFarmer(farmer));
    }

    /**删除逻辑：
     * 当不为户主走默认路径remove
     * 当为户主时走路径removeHouseholder
     * 内部会判断人口数，若人口数为1，内部进行操作，返回1；若人口大于1，则返回人口数
     * 前端接收若大于1，则进行转移户主页面的跳转，执行transfer接口接收信息；
     * 信息输入完成后调transferHouseholder进行转移
     */





        /**
         * 删除农户基本信息,ids只可能传过来int型而不是数组，不为户主
         */
        @PreAuthorize("@ss.hasPermi('NH:farmer:remove')")
        @Log(title = "删除农户基本信息", businessType = BusinessType.DELETE)
        @DeleteMapping("/{ids}")
        public AjaxResult remove(@PathVariable("ids") Integer ids)
        {
            return toAjax(farmerService.deleteFarmerById(ids));
        }

//    /**
//     * 删除农户基本信息,为户主
//     */
//    @PreAuthorize("@ss.hasPermi('NH:farmer:removeHouseholder')")
//    @Log(title = "删除农户基本信息(户主)", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{id}")
//    public AjaxResult removeHouseholder(@PathVariable("id") Integer id)
//    {
//        return toAjax(farmerService.deleteHouseholderById(id));
//    }
//
//    /**
//     * 转移户主页面所需的信息
//     */
//    @PreAuthorize("@ss.hasPermi('NH:transfer:list')")
//    @GetMapping("/transfer/{id}")
//    public Map transfer(@PathVariable("id") Integer id, Map mmap)
//    {
//        Farmer farmer = farmerService.selectFarmerById(id); //一定为户主
//        String fid = farmer.getFid();
//        //显示自然村需要
//        BaseFamilyInfo baseFamilyInfo = baseFamilyInfoService.selectBaseFamilyInfoByFid(fid);
//        mmap.put("baseFamilyInfo", baseFamilyInfo);
//        List<Farmer> list = farmerService.selectMember(fid);
//        mmap.put("member",list);
//        mmap.put("size",list.size());
//        return mmap;
//    }
//
//
//    /**
//     *转移户主，并且删除原户主
//     */
//    @PreAuthorize("@ss.hasPermi('NH:transfer:fix')")
//    @Log(title = "转移户主", businessType = BusinessType.UPDATE)
//    @PostMapping()
//    // str为str = str + farmers[i].id + "," +  farmers[i].relationship + ",";形式的数据，farmers为除户主外其他户成员集合的数组
//    public AjaxResult transferHouseholder(@PathVariable("fid") String fid,@PathVariable("oldName") String oldName,@PathVariable("newName") String newName,@PathVariable("str") String str)
//    {
//        return toAjax(farmerService.transferHouseholder(fid,oldName,newName,str));
//    }
}
